<template>
  <div class="member-order">
    <!-- tab组件 -->
    <XtxTables v-model="activeName" @tab-click="tabClick">
      <XtxTablesPannel v-for="item in orderStatus" :key="item.name" :label="item.label" :name="item.name">
      </XtxTablesPannel>
    </XtxTables>
    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
      @on-cancel="handlerFn"
      v-for="item in orderList"
      :key="item.id"
      :order="item"
      @on-delete="handlerDelete"
      @on-confirm="handlerConfirm"
      @on-logistics="handlerLogistic"
       />
    </div>
    <!-- 分页组件 -->
    <XtxPagination
    v-if="total > 0"
    :current-page="reqParams.page"
    :pageSize="reqParams.pageSize"
    :total="total"
    @currentChange="reqParams.page=$event"
    ></XtxPagination>
    <!-- 取消原因 -->
    <OrderCancel  ref="orderCancelCom"/>
    <!-- 查看物流 -->
    <OrderLogistic  ref="orderLogistic"/>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import OrderItem from './component/order-item.vue'
import { findOrderList, deleteOrder, confirmOrder } from '@/api/order'
import OrderCancel from './component/order-cancel.vue'
import OrderLogistic from './component/order-logistic.vue'
import Confirm from '@/components/libaray/Confirm'
import Message from '@/components/libaray/Message'
export default {
  name: 'MembeOrder',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistic
  },
  setup () {
    const activeName = ref('all')
    // 获取数据
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    const orderList = ref([])
    const loading = ref(false)
    const total = ref(0)
    // 筛选条件变化，重新加载
    const getOrderList = () => {
      loading.value = true
      findOrderList(reqParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
      })
    }
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true })
    // Tab切换
    // 点击选项卡
    const tabClick = ({ index }) => {
      // console.log(index)
      reqParams.page = 1
      reqParams.orderState = index
    }
    // 删除订单
    const handlerDelete = (order) => {
      Confirm({ text: '亲,您确认删除该订单吗?' }).then(() => {
        deleteOrder(order.id).then(() => {
          Message({ type: 'success', message: '删除成功' })
          getOrderList()
        })
      }).catch(err => {
        console.log(err)
      })
    }

    return { activeName, orderStatus, orderList, tabClick, loading, total, reqParams, ...useCancel(), handlerDelete, ...useConfirm(), ...useLogistics() }
  }
}

// 查看物流逻辑
export const useLogistics = () => {
  const orderLogistic = ref(null)
  const handlerLogistic = (order) => {
    orderLogistic.value.open(order)
  }
  return { handlerLogistic, orderLogistic }
}
// 确认收货逻辑
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '亲,您确认收货吗?确认后货款将打给卖家' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ type: 'success', message: '确认收货成功' })
        // 待收货  --》 待评价
        order.orderState = 4
      })
    }).catch(err => {
      console.log(err)
    })
  }

  return { handlerConfirm }
}
// 取消订单逻辑
export const useCancel = () => {
  // 组件实例
  const orderCancelCom = ref(null)
  // 点击取消
  const handlerFn = (order) => {
    orderCancelCom.value.open(order)
  }

  return { handlerFn, orderCancelCom }
}
</script>

<style scoped lang="less">
.member-order {
 height: 100%;
 background-color: #fff;
 .order-list {
  background: #fff;
  padding: 20px;
  min-height: 400px;
  position: relative;
  .loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255,255,255,.9) url(../../../assets/images/loading.gif) no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
}
}
</style>
